using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace ScrollingA2DBackground
{
    public class Background
    {

        //Positions of each background
        public int[] xPositions;
        
        //velocity of the background
        public Vector2 velocity;
        
        //the image textures of the background
        public Texture2D[] backTexture;

        //the dimensions of the client window
        //int[] dimension;

        private int[] dimension;

        public int[] Dimension
        {
            get { return dimension; }
            set { dimension = value; }
        }
	

        //Temporary variable used for testing purposes
        private int playerXVel;

        //background names properties
        private string[] backgroundNames;
        public string[] BackgroundNames
        {
            get { return backgroundNames; }
            set { backgroundNames = value; }
        }


        /// <summary>
        /// Allows the creation of a background object.
        /// Its purpose is to dynamically create a background with the ability to create the illusion
        /// of movement.
        /// </summary>
        public Background( string[] backgrounds, int[] dim )
        {
            //Creates as many texture background names as there are names
            this.BackgroundNames = backgrounds;
            this.dimension = new int[2];
            this.dimension[0] = dim[0]; 
            this.dimension[1] = dim[1];
            this.playerXVel = 5;

        }//End of Background constructor



        /// <summary>
        /// Initializes the background
        /// </summary>
        protected void Initialize()
        {

        }//End of initialize method




        /// <summary>
        /// If there is anything to load, it calls the loading method.
        /// </summary>
        public void LoadGraphicsContent( ContentManager content )
        {
            this.backTexture = new Texture2D[ this.backgroundNames.Length ];
            this.xPositions = new int[this.backgroundNames.Length];
            //Load the background images into the texture object
            for (int i = 0; i < backgroundNames.Length; i++)
            {
                this.backTexture[i] = content.Load<Texture2D>( this.backgroundNames[i] ) as Texture2D;

                //Initializes all the background positions to their real positions
                //this.xPositions[i] = 1;
                this.xPositions[i] = this.dimension[0] * i;
            }
        }//End of LoadGraphicsContent




        /// <summary>
        /// Updates the position of the moving background, if it moves
        /// </summary>
        public void Update( GameTime gameTime )
        {
            //Update code here
            this.moveBackground();
        }

        
        
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected void moveBackground()
        {
            for( int i = 0; i < this.xPositions.Length; i++ )
            {
                this.xPositions[i] -= this.playerXVel;

                if (this.xPositions[i] <= -this.Dimension[0])
                {
                    this.xPositions[i] = this.Dimension[0] * 4;
                }
            }
        }//End of Update



        /// <summary>
        /// Draws the background at the updated position 
        /// </summary>
        protected void Draw( GameTime gameTime ) 
        { 
            //Drawing code here

        }//End of Draw method
    }
}